/**
 * Solution: 
 * 
 * the only paint operation required is to paint the entire row or column. 
 * However, we could not paint the row/col if there exists a 'W'. 
 * So we only need to count the all black rows and cols.
 * 
 * But we also need to reduce the duplicates if they all are black.
 */

/**
 * @author antonio081014
 * @since Dec 14, 2011, 11:29:54 AM
 */

public class MonochromaticBoard {

	public String[] grid;
	public int M;
	public int N;

	public int theMin(String[] board) {
		grid = board.clone();
		N = board.length;
		M = board[0].length();
		if (allBlack())
			return Math.min(N, M);
		int ret = 0;
		for (int i = 0; i < N; i++)
			if (checkRow(i))
				ret++;
		for (int i = 0; i < M; i++)
			if (checkCol(i))
				ret++;
		return ret;
	}

	public boolean checkRow(int r) {
		for (int i = 0; i < M; i++)
			if (grid[r].charAt(i) == 'W')
				return false;
		return true;
	}

	public boolean checkCol(int c) {
		for (int i = 0; i < N; i++)
			if (grid[i].charAt(c) == 'W')
				return false;
		return true;
	}

	public boolean allBlack() {
		for (int i = 0; i < N; i++) {
			for (int j = 0; j < M; j++)
				if (grid[i].charAt(j) == 'W')
					return false;
		}
		return true;
	}

	// <%:testing-code%>
}
// Powered by [KawigiEdit] 2.0!
